import {AfterViewInit, Component, OnInit, ViewChild} from '@angular/core';
import {getProperty, MessageService, ModalComponent, PaginationService, Table} from '../../../../../ng-tools';
import {TicketService} from '../../../../services/api/ticket.service';
import {Router} from '@angular/router';
import {RobotManagerFormatterService} from '../../../../services/formatter/robot-manager.service';
import {RobotProduceComponent} from '../robot-produce/robot-produce.component';
import {RobotEditComponent} from '../robot-edit/robot-edit.component';
import {HttpParams} from '@angular/common/http';
import {ChargeStyleFormatterService} from '../../../../services/formatter/charge-style-formatter.service';
import {ChargeStyleAddAndEditComponent} from '../charge-style-add-and-edit/charge-style-add-and-edit.component';

@Component({
  selector: 'charge-style',
  templateUrl: './charge-style.component.html',
  styleUrls: ['./charge-style.component.scss']
})
export class ChargeStyleComponent implements OnInit, AfterViewInit {

  @ViewChild('modal') private modal: ModalComponent;
  breadcrumb = [
    {name: '充值分类', cssIcon: 'fa fa-fw fa-gg-circle'}
  ];
  deleteType;
  page = 1;
  editRobotData;
  deleteRobotData;
  bankPagenation = new PaginationService(null, null);
  airpayPagenation = new PaginationService(null, null);
  wecahtPagenation = new PaginationService(null, null);
  bankTable = new Table({
    headers: this.chargeStyleFormatter.bankFormatter,
    buttons: [
      {
        btnClass: 'btn btn-danger btn-sm mr-1 px-1 py-0',
        iconClass: 'fa fa-fw fa-pencil-square',
        text: '编辑',
        isShow: true,
        click: (e) => {
          this.addAndEditData(1, e);
        }
      },
      {
        btnClass: 'btn btn-danger btn-sm mr-1 px-1 py-0',
        iconClass: 'fa fa-fw fa-trash',
        text: '删除',
        isShow: true,
        click: (e) => {
          this.deleteRobotData = e;
          this.deleteType = 1;
          this.deleteData();
        }
      }
    ]
  });
  airpayTable = new Table({
    headers: this.chargeStyleFormatter.formatter,
    buttons: [
      {
        btnClass: 'btn btn-danger btn-sm mr-1 px-1 py-0',
        iconClass: 'fa fa-fw fa-pencil-square',
        text: '编辑',
        isShow: true,
        click: (e) => {
          this.addAndEditData(2, e);
        }
      },
      {
        btnClass: 'btn btn-danger btn-sm mr-1 px-1 py-0',
        iconClass: 'fa fa-fw fa-trash',
        text: '删除',
        isShow: true,
        click: (e) => {
            this.deleteType = 2;
          this.deleteRobotData = e;
          this.deleteData();
        }
      }
    ]
  });
  wechatTable = new Table({
    headers: this.chargeStyleFormatter.formatter,
    buttons: [
      {
        btnClass: 'btn btn-danger btn-sm mr-1 px-1 py-0',
        iconClass: 'fa fa-fw fa-pencil-square',
        text: '编辑',
        isShow: true,
        click: (e) => {
          this.addAndEditData(3, e);
        }
      },
      {
        btnClass: 'btn btn-danger btn-sm mr-1 px-1 py-0',
        iconClass: 'fa fa-fw fa-trash',
        text: '删除',
        isShow: true,
        click: (e) => {
            this.deleteType = 3;
          this.deleteRobotData = e;
          this.deleteData();
        }
      }
    ]
  });
  constructor(private message: MessageService,
              private ticketService: TicketService,
              private router: Router,
              private chargeStyleFormatter: ChargeStyleFormatterService,
  ) {
  }

  ngOnInit() {
    this.getBankInfo();
    this.getAirpayInfo();
    this.getWechatInfo();

  }
  deleteData() {
      const options = {
          id: 'deleteModal',
          title: '删除通道',
          cssIcon: 'fa fa-fw fa-warning',
          size: 'sm',
          theme: 'danger',
          backdrop: 'static',
          data: '是否此通道？',
          closeBtn: true,
          confirmBtn: true
      };
      this.modal.show(options);
  }
  addAndEditData(type, data) {
    this.modal.show({
      title: '添加通道',
      cssIcon: 'fa fa-fw fa-plus-circle',
      dynamicComponent: {
        component: ChargeStyleAddAndEditComponent,
          data: {type: type, data: data},
        events: {
          success: () => {
            this.modal.hide();
            switch (type) {
                case 1:
                    this.getBankInfo();
                    break;
                case 2:
                    this.getAirpayInfo();
                    break;
                case 3:
                    this.getWechatInfo();
                    break;
            }
            // this.getInfo(1);
          }
        },
      },
      data: null
    });
  }
  modalEvenHandler() {
    this.modal.event.subscribe({
      next: (event) => {
        if (event.name === 'confirm' && event.id === 'deleteModal') {
          this.deleteRobot(this.deleteType);
        }
      }
    });
  }
  deleteRobot(type) {
    this.ticketService.post(this.ticketService.urls.delAccountInfo, {id: this.deleteRobotData.id}, {
      observe: 'response',
      params: new HttpParams()
          .append('web_token', localStorage.getItem('token'))}).subscribe((resp) => {
      if (resp.body.success) {
        this.modal.hide();
          switch (type) {
              case 1:
                  this.getBankInfo();
                  break;
              case 2:
                  this.getAirpayInfo();
                  break;
              case 3:
                  this.getWechatInfo();
                  break;
          }
      }
    });
  }
  getBankInfo() {
    this.bankPagenation.simpleFormatter = this.bankTable.options.headers;
    this.ticketService.get(this.ticketService.urls.getAccounts, {
      observe: 'response',
      params: new HttpParams()
          .append('web_token', localStorage.getItem('token'))
          .append('type', '1')}).subscribe((resp) => {
      // console.log(resp);
      if (resp.body.success) {
        this.bankPagenation.page.results = resp.body.returnValue;
        this.bankPagenation.formatData();
      } else {
        this.router.navigateByUrl('/login').catch();
      }
    });
  }
  getAirpayInfo() {
    this.airpayPagenation.simpleFormatter = this.airpayTable.options.headers;
    this.ticketService.get(this.ticketService.urls.getAccounts, {
      observe: 'response',
      params: new HttpParams()
          .append('web_token', localStorage.getItem('token'))
          .append('type', '2')}).subscribe((resp) => {
      // console.log(resp);
      if (resp.body.success) {
        this.airpayPagenation.page.results = resp.body.returnValue;
        this.airpayPagenation.formatData();
      } else {
        this.router.navigateByUrl('/login').catch();
      }
    });
  }
  getWechatInfo() {
    this.wecahtPagenation.simpleFormatter = this.wechatTable.options.headers;
    this.ticketService.get(this.ticketService.urls.getAccounts, {
      observe: 'response',
      params: new HttpParams()
          .append('web_token', localStorage.getItem('token'))
          .append('type', '3')}).subscribe((resp) => {
      if (resp.body.success) {
        this.wecahtPagenation.page.results = resp.body.returnValue;
        this.wecahtPagenation.formatData();
        this.message.sendMessage('page load progress', this);
      } else {
        this.router.navigateByUrl('/login').catch();
      }
    });
  }
  get getProperty() {
    return getProperty;
  }
  // getAvatar() {
  //   this.ticketService.get(this.ticketService.urls.userAvatarDownloader, {
  //     observe: 'response',
  //     params: new HttpParams()
  //         .append('web_token', localStorage.getItem('token'))
  //         .append('action', 'ad')
  //         .append('user_uid', '51942797')
  //         .append('enforceDawnload', '1')}).subscribe((resp) => {
  //     console.log(resp);
  //   });
  // }
  // firstPage() {
  //   this.getInfo(1);
  // }
  // previousPage() {
  //   if (this.page > 1) {
  //     this.page = this.page - 1;
  //   } else {
  //     this.page = 1;
  //   }
  //   this.getInfo(this.page);
  // }
  // nextPage() {
  //   if (this.page < this.pagenation.page.totalPage) {
  //     this.page = this.page + 1;
  //   } else {
  //     this.page = this.pagenation.page.totalPage;
  //   }
  //   this.getInfo(this.page);
  // }
  // lastPage() {
  //   this.getInfo(this.pagenation.page.totalPage);
  // }
  ngAfterViewInit(): void {
    this.modalEvenHandler();
    this.message.sendMessage('breadcrumb changed', this, this.breadcrumb);
    this.message.sendMessage('page load progress', this, '300ms');
  }

}
